$(document).ready(function() {
   
   
     $('button[name="button"]').click(function() { 
	 var success1 = verificaNome($('input[name="name"]').val());
     var success2 = verificaEmail($('input[name="nameEmail"]').val());
     var success3 = verificaPass($('input[name="pass"]').val());
     var success4 = verificaPass2($('input[name="pass2"]').val());
     
     if ((success1) && (success1) && (success3) && (success4))
      return true;
      else 
      return false
    });
    
    $('input[name="name"]').focus(function() {
	   
    verificaNome($(this).val());

    });
    
    
   	$('input[name="name"]').keyup(function() {
   	    
        verificaNome($(this).val());
        
        });
    
    
    $('input[name="name"]').blur(function() {
        verificaNome($(this).val());
       
                     
    });
    
    
    
   	$('input[name="nameEmail"]').focus(function() {
	   
    verificaEmail($(this).val());

    });
    
    
   	$('input[name="nameEmail"]').keyup(function() {
   	    
        verificaEmail($(this).val());
        
        });
    
    
    $('input[name="nameEmail"]').blur(function() {
        verificaEmail($(this).val());
       
                     
    });
    
     $('input[name="pass"]').focus(function() {
	   
    verificaPass($(this).val());

    });
    
    
   	$('input[name="pass"]').keyup(function() {
   	    
        verificaPass($(this).val());
        
            $('input[name="pass2"]').val('');
             $('tr.pass2Tr').find('.label-box.good').css("display","none");
             $('tr.pass2Tr').find('.label-box.error').css("display","none");
             $('tr.pass2Tr').find('.label-box.error').html("");
             $('tr.pass2Tr').find('.label-box.info').css("display","block");
            
        
        });
    
    
    $('input[name="pass"]').blur(function() {
        verificaPass($(this).val());
       
                     
    });
    
    
         $('input[name="pass2"]').focus(function() {
        verificaPass2($(this).val());

    });
    
    
   	$('input[name="pass2"]').keyup(function() {
   	    
        verificaPass2($(this).val());
        
        });
    
    
    $('input[name="pass2"]').blur(function() {
        verificaPass2($(this).val());
       
                     
    });
    

function verificaNome (str){



    if(str == ''){    
           
         
        $('tr.nameTr').find('.label-box.good').css("display","none");
        $('tr.nameTr').find('.label-box.info').css("display","block");
        $('tr.nameTr').find('.label-box.error').css("display","none");
        return false;
        }
        else{
          
        $('tr.nameTr').find('.label-box.good').css("display","block");
        $('tr.nameTr').find('.label-box.error').css("display","none");
        $('tr.nameTr').find('.label-box.info').css("display","none");
        return true;
        
        }

}

function verificaEmail (str){



    if(str != ''){    
        
       if (checkMail(str)) {
        
    var dataString = 'email='+ str;
    $.ajax({
    type: "POST",
    url: "lib/checkForm.php",
    data: dataString,
    success: function(result){
        
        
        if (result > 0 ){
          
        $('tr.emailTr').find('.label-box.good').css("display","none");
        $('tr.emailTr').find('.label-box.error').css("display","block");
        $('tr.emailTr').find('.label-box.error').html("Email already exist");
        $('tr.emailTr').find('.label-box.info').css("display","none");
        return false;
        }
        else{
         
         
        $('tr.emailTr').find('.label-box.good').css("display","block");
        $('tr.emailTr').find('.label-box.error').css("display","none");
        $('tr.emailTr').find('.label-box.info').css("display","none");
        return true;
            
        }
}

})

    
    }
        else{
        
        
        $('tr.emailTr').find('.label-box.good').css("display","none");
        $('tr.emailTr').find('.label-box.error').css("display","block");
        $('tr.emailTr').find('.label-box.info').css("display","none");
        $('tr.emailTr').find('.label-box.error').html("Invalid email");
        return false;
        
    }

    

}
    else{
        $('tr.emailTr').find('.label-box.good').css("display","none");
        $('tr.emailTr').find('.label-box.error').css("display","none");
        $('tr.emailTr').find('.label-box.info').css("display","block");
        return false;
        
    }
    }


function verificaPass (str){


    
    if(str == ''){    
           
         
        $('tr.passTr').find('.label-box.good').css("display","none");
        $('tr.passTr').find('.label-box.info').css("display","block");
        $('tr.passTr').find('.label-box.error').css("display","none");
        return false;
        }
        else{
            
           
            
            if(str.length < 6){
                
             $('tr.passTr').find('.label-box.good').css("display","none");
             $('tr.passTr').find('.label-box.error').css("display","block");
             $('tr.passTr').find('.label-box.error').html("Too short");
             $('tr.passTr').find('.label-box.info').css("display","none");
             return false;
                
            }
            else{
        $('tr.passTr').find('.label-box.good').css("display","block");
        $('tr.passTr').find('.label-box.error').css("display","none");
        $('tr.passTr').find('.label-box.info').css("display","none");
        return true;
        }
        
        }

}

function verificaPass2 (str){

    var pass1 = $('input[name="pass"]').val();
    if ((pass1 != '') && (pass1.length >= 6)){
        
    if (str == ''){    
           
         
        $('tr.pass2Tr').find('.label-box.good').css("display","none");
        $('tr.pass2Tr').find('.label-box.info').css("display","block");
        $('tr.pass2Tr').find('.label-box.error').css("display","none");
        return false;
        }
        else{
           
            
            if(str != pass1){
                
             $('tr.pass2Tr').find('.label-box.good').css("display","none");
             $('tr.pass2Tr').find('.label-box.error').css("display","block");
             $('tr.pass2Tr').find('.label-box.error').html("Passwords do not match");
             $('tr.pass2Tr').find('.label-box.info').css("display","none");
             return false;
                
            }
            else{
          
        $('tr.pass2Tr').find('.label-box.good').css("display","block");
        $('tr.pass2Tr').find('.label-box.error').css("display","none");
        $('tr.pass2Tr').find('.label-box.info').css("display","none");
        return true;
        }
        
        }
        }
        else{
            
            
              $('tr.pass2Tr').find('.label-box.good').css("display","none");
             $('tr.pass2Tr').find('.label-box.error').css("display","block");
             $('tr.pass2Tr').find('.label-box.error').html("Invalid password");
             $('tr.pass2Tr').find('.label-box.info').css("display","none");  
             return false;
            
        }

}


function checkMail(mail){
        var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
        if(typeof(mail) == "string"){
                if(er.test(mail)){ return true; }
        }else if(typeof(mail) == "object"){
                if(er.test(mail.value)){ 
                                        return true; 
                                }
        }else{
                return false;
                }
}
   
   
    
});






